﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string conString = "server=.;database=Blogs;uid=sa;pwd=1";
            SqlConnection sqlConnection = new SqlConnection(conString);
            string cmdString = "select * from Articles";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, sqlConnection);
            DataTable dt = new DataTable();
            sqlConnection.Open();
            dataAdapter.Fill(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect ;
            dataGridView1.AllowUserToAddRows = false;
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Author like '%{0}%'", name);
            var dt = DbHeiper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
          
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Editform editform = new Editform();
            var res = editform.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                var dt = DbHeiper.GetDataTable(sqlstu);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("失败了");
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1 .SelectedRows .Count > 0)
            {
                var id =(int ) dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title =(string ) dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author =(string ) dataGridView1.SelectedRows[0].Cells["Author"].Value;
  
                Editform editform = new Editform(id,title,author   );
                var res = editform.ShowDialog();
                if(res ==DialogResult.Yes)
                {
                    var name = textBox1.Text;
                    var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                    var dt = DbHeiper.GetDataTable(sqlstu);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("失败了");
                }
            }
            else
            {
                MessageBox.Show("您未选择任何的数据行", "提示");
            }
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if(dataGridView1 .SelectedRows .Count > 0)
            {
                var nee = dataGridView1.SelectedRows[0];
                var id = (int)nee.Cells["Id"].Value;
                var Deleteprompt = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if(Deleteprompt .Equals (DialogResult .Yes))
                {
                    var sql = string.Format("delete from Articles where id={0}", id);
                    var neeass = DbHeiper.AddOrUpdaterOrDelete(sql);
                    if(neeass > 0)
                    {
                        var name = textBox1.Text;
                        var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                        var dt = DbHeiper.GetDataTable(sqlstu);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功");
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择要删除的项目");
            }
        }
    }
}
